package com.jeesite.modules.dp.web.xinwen;

import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.jeesite.common.config.Global;
import com.jeesite.common.entity.Page;
import com.jeesite.common.web.BaseController;
import com.jeesite.modules.dp.entity.xinwen.DpXinwen;
import com.jeesite.modules.dp.service.xinwen.DpXinwenService;

/**
 * 新闻管理Controller
 * @author hancing
 * @version 2024-12-19
 */
@Controller
@RequestMapping(value = "${adminPath}/dp/xinwen/dpXinwen")
public class DpXinwenController extends BaseController {

	@Autowired
	private DpXinwenService dpXinwenService;
	
	/**
	 * 获取数据
	 */
	@ModelAttribute
	public DpXinwen get(String id, boolean isNewRecord) {
		return dpXinwenService.get(id, isNewRecord);
	}
	
	/**
	 * 查询列表
	 */
	@RequiresPermissions("dp:xinwen:dpXinwen:view")
	@RequestMapping(value = {"list", ""})
	public String list(DpXinwen dpXinwen, Model model) {
		model.addAttribute("dpXinwen", dpXinwen);
		return "modules/dp/xinwen/dpXinwenList";
	}
	
	/**
	 * 查询列表数据
	 */
	@RequiresPermissions("dp:xinwen:dpXinwen:view")
	@RequestMapping(value = "listData")
	@ResponseBody
	public Page<DpXinwen> listData(DpXinwen dpXinwen, HttpServletRequest request, HttpServletResponse response) {
		dpXinwen.setPage(new Page<>(request, response));
		Page<DpXinwen> page = dpXinwenService.findPage(dpXinwen);
		return page;
	}

	/**
	 * 查看编辑表单
	 */
	@RequiresPermissions("dp:xinwen:dpXinwen:view")
	@RequestMapping(value = "form")
	public String form(DpXinwen dpXinwen, Model model) {
		model.addAttribute("dpXinwen", dpXinwen);
		return "modules/dp/xinwen/dpXinwenForm";
	}

	/**
	 * 保存数据
	 */
	@RequiresPermissions("dp:xinwen:dpXinwen:edit")
	@PostMapping(value = "save")
	@ResponseBody
	public String save(@Validated DpXinwen dpXinwen) {
		dpXinwenService.save(dpXinwen);
		return renderResult(Global.TRUE, text("保存新闻成功！"));
	}
	
	/**
	 * 删除数据
	 */
	@RequiresPermissions("dp:xinwen:dpXinwen:edit")
	@RequestMapping(value = "delete")
	@ResponseBody
	public String delete(DpXinwen dpXinwen) {
		dpXinwenService.delete(dpXinwen);
		return renderResult(Global.TRUE, text("删除新闻成功！"));
	}
	
}